package com.mall.shopping.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.ProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @User: Yr
 * @Date: 2021/12/13
 * @Description:
 */

@Slf4j
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductDetailConverter converter;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = converter.item2Dto(item);
            productDetailDto.setSubTitle("");
            productDetailDto.setDetail("");



//            productDetailDto.setProductImageSmall(item.getImages());




            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDetailDto(productDetailDto);
        } catch (ValidateException e) {
            //参数异常
            e.printStackTrace();
            response.setCode(ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getCode());
            response.setMsg(ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getMessage());
        } catch (Exception e) {
            //数据库异常
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();
        try {
            request.requestCheck();

//            List<Item> collect = itemList.stream().limit(8).sorted((x, y) -> x.getId().compareTo(y.getId())).collect(Collectors.toList());
//            Integer total = itemMapper.selectTotalByRequest(request);
            List<Item> itemList = itemMapper.selectItemByRequest(request);

            //page是第几页，size是每页的数量，offset是跳过的数量
            Integer page = request.getPage();
            Integer size = request.getSize();
            int offset = (page - 1) * size;

            //total是数据库查询结果的数量，remain是剩余的数量
            int total = itemList.size();
            int remain = total - offset;
            List<Item> realItemList = null;

            //如果剩余数量小于每页数量，则返回的list的数量小于size，否则等于size
            if (remain <= size) {
                realItemList = itemList.subList(offset, total);
            } else {
                realItemList = itemList.subList(offset, offset + size);
            }


            List<ProductDto> productDtoList = productConverter.items2Dto(realItemList);

            allProductResponse.setData(productDtoList);
            allProductResponse.setTotal(new Long(total));
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("ProductServiceImpl.getAllProduct occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        try {
            Panel panel = panelMapper.selectRecommendPanel();
            Integer panelId = panel.getId();
            List<PanelContentItem> panelContentItemList = panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);

            PanelDto panelDto = contentConverter.panen2Dto(panel);
            List<PanelContentItemDto> panelContentItemDtoList = contentConverter.panelContentItem2Dto(panelContentItemList);
            panelDto.setPanelContentItems(panelContentItemDtoList);

            //TODO 参考一下别人的代码
            Set<PanelDto> panelDtoSet = new HashSet<>();
            panelDtoSet.add(panelDto);

            recommendResponse.setPanelContentItemDtos(panelDtoSet);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("ProductServiceImpl.getRecommendGoods occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }
        return recommendResponse;
    }

    @Override
    public Map getItem(Long productId) {
        Item item = new Item();
        item.setId(productId);
        Item item1 = itemMapper.selectByPrimaryKey(item);
        HashMap<String, String> map = new HashMap<>();
        map.put("title",item1.getTitle());
        map.put("image",item1.getImage());

        return map;



    }
}
